package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.excelption.DBException;
import com.baichang.framework.model.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.taoshunda.dao.PushDao;
import com.baichang.taoshunda.entity.Push;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class PushDaoImpl extends BaseDaoImpl<Push> implements PushDao {

    public long save(Push push) {
        return super.save(push);
    }

    /**
     * @param
     * @param nowPage
     * @param pageSize
     * @return
     * @Athour:zhaoguopeng
     * @created:2017.8.3 获取消息列表APP
     */
    @Override
    public List<Map<String, Object>> searchPushList(Integer nowPage, Integer pageSize) {
        String sql = " SELECT * FROM {table} ORDER BY create_time DESC ";
        return listForMapList(sql, nowPage, pageSize);
    }

    /**
     * 消息详情
     *
     * @param id
     * @return
     */
    @Override
    public Push getMessageById(Integer id) {
        String sql = "SELECT title,remark,content,picture FROM t_push WHERE id =?";
        return queryForBean(sql, id);
    }

    /**
     * 获取消息列表
     */
    @Override
    public Page<Map<String, Object>> pushQuery(int nowPage, int pageSize, String tuisongren, String xjstart, String xjend, String tuisongtitle) throws DBException {
        StringBuffer sb = new StringBuffer();
        List<Object> param = new ArrayList<Object>();

        sb.append("select * from t_push a where 1=1 ");

        if (StringUtils.isNotBlank(xjstart) && StringUtils.isNotBlank(xjend)) {
            sb.append("and date_format(a.create_time,'%Y-%m-%d') between ? and ? ");
            param.add(xjstart);
            param.add(xjend);
        }
        if (StringUtils.isNotBlank(tuisongren)) {
            if ("1".equals(tuisongren)) {
                sb.append(" and a.users like \'%1%\'");
            } else if ("2".equals(tuisongren)) {
                sb.append(" and a.users not like \'%1%\' and a.users!=\'全部\'");
            } else {
                sb.append(" and a.users=\'全部\'");
            }
        }
        if (StringUtils.isNotBlank(tuisongtitle)) {
            sb.append("and a.title like ? ");
            param.add("%" + tuisongtitle + "%");
        }
        sb.append("order by a.create_time desc ");


        return pageForMapList(sb.toString(), nowPage, pageSize, param.toArray());
    }


    /**
     * 删除该推送消息  用到
     */
    @Override
    public boolean deletePushById(int id) throws DBException {
        System.out.println(id);
        String sql = "id=?";

        return delete(sql, id) > 0;
    }

    /**
     * 删除push
     *
     * @param id 编号
     * @return
     */
    public int delete(int id) {
        return delete(" id=? ", id);
    }

    public Push findAllPushMsg() {
        String sql = " select * from t_push order by create_time desc limit 0,1 ";
        return queryForBean(sql);
    }

    /**
     * 查看商家的消息列表
     *
     * @param account  账号
     * @param nowPage  当前页
     * @param pageSize 每页大小
     * @return
     */
    @Override
    public List<Map<String, Object>> findBusinessPushList(String account, Integer nowPage, Integer pageSize) {
        String sql = " SELECT * FROM t_push WHERE users LIKE ? OR users LIKE '%全部%' OR users LIKE '%商家端%' ORDER BY create_time DESC ";
        return listForMapList(sql, nowPage, pageSize, "%" + account + "%");
    }

    /**
     * 查看用户的的消息列表
     *
     * @param account  账号
     * @param nowPage  当前页
     * @param pageSize 每页大小
     * @param userId
     * @return
     */
    @Override
    public List<Map<String, Object>> findUserPushList(String account, Integer nowPage, Integer pageSize, Integer userId) {
        String sql = " SELECT p.* FROM t_push p INNER JOIN t_user u ON u.id = ? WHERE users LIKE ? OR users LIKE '%全部%' OR users LIKE '%用户端%' AND TO_DAYS(p.create_time) > TO_DAYS(u.created) ORDER BY create_time DESC ";
        return listForMapList(sql, nowPage, pageSize, userId, "%" + account + "%");
    }

    /**
     * 接口查看配送员的消息列表
     *
     * @param account  配送员账号
     * @param nowPage  当前页
     * @param pageSize 每页大小
     * @return
     */
    @Override
    public List<Map<String, Object>> findPushList(String account, Integer nowPage, Integer pageSize) {
        String sql = " SELECT * FROM t_push WHERE users LIKE ? OR users LIKE '%全部%' OR users LIKE '%配送端%' ORDER BY create_time DESC ";
        return listForMapList(sql, nowPage, pageSize, "%" + account + "%");
    }


}